﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample126;

public class TransformSystem : ComponentSystem
{
    int operatorT = 1;
    float span = 0.00005f;
    float factor = 0.0f;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="contextManager"></param>
    public TransformSystem()
    {
    }


    public override void Initialization()
    {
        base.Initialization();
    }

    public override void Update(TimeSpan timeSpan)
    {
        factor += operatorT * span * (float)timeSpan.TotalMilliseconds;
        if (factor > 1.0f)
        {
            factor = 1.0f;
            operatorT = -operatorT;
        }
        else if (factor < 0)
        {
            factor = 0;
            operatorT = -operatorT;
        }
        ContextManager.Foreach<OBJGroup>((group) =>
        {
            Context context = ContextManager.GetContext(group.Entity);
            var obj = context.GetContextRenderObject();
            var tex = obj.GetComponents<ITexture>().Where(x => x.Binding == 0).FirstOrDefault();
            tex.UpdateTextureMemoryData(factor);
            ref MVPMatrix mvp = ref context.GetMVPMatrix();
            mvp.ModelMatrix = Matrix4x4.CreateTranslation(group.Transform.Position);
            context.PushToRenderGroup();
        });
    }
}

public struct OBJGroup
{
    public Entity Entity;
    public TransformComponent Transform;
}
